//
//  PosterView.m
//  HuiWenMovieApp
//
//  Created by ios on 15/7/24.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "PosterView.h"
#import "UIViewExt.h"
#define kwth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
#define kHeaderViewH 136
#define kIndexCollectionViewH 100
#define kTabBarHeight 49
#define kBottomTitleLableH 35
#define kBottomTitleLableY (kheight-kTabBarHeight-kBottomTitleLableH)
#define kHeaderOffH   36
#define kPosterColletionViewY  (kHeaderViewH-kHeaderOffH)
#define kPosterColletionViewH  (kheight-kPosterColletionViewY-kTabBarHeight-kBottomTitleLableH)

@implementation PosterView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self configureMainCollectionView];
    [self configureCoverView];
    [self configureHeadView];
    [self configureBottomTitleLabel];
    [self configureSwipeGesture];
    return  self;
}
- (void)configureSwipeGesture
{
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDownAction)];
    
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self addGestureRecognizer:swipeDown];

}

- (void)configureCoverView
{
    _coverView = [[UIControl alloc]initWithFrame:_mainCollectionView.frame];
    
    _coverView.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8];
    
    [_coverView addTarget:self action:@selector(coverViewAction) forControlEvents:UIControlEventTouchUpInside];
    
    _coverView.hidden = YES;
    [self addSubview:_coverView];
}

- (void)configureHeadView
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;

    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, -kHeaderOffH, kwth, kHeaderViewH)];
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:_headView.bounds];
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    bgImageView.image = image;
    [_headView addSubview:bgImageView];
    
    _collectionView = [[PosterCollectionView alloc] initWithFrame:CGRectMake(0, 0, kwth, kIndexCollectionViewH)collectionViewLayout:flowLayout];
    _collectionView.cellWidth = kwth/5;
    _collectionView.cellHeight = kIndexCollectionViewH;
    
    
    UIButton *buttonForPushAndPull = [[UIButton alloc] initWithFrame:CGRectMake((kwth - 20)/2, kHeaderViewH - 20, 20, 20)];
    buttonForPushAndPull.tag = 100;
    [buttonForPushAndPull addTarget:self action:@selector(actionForPullAndPush:) forControlEvents:UIControlEventTouchUpInside];
    [buttonForPushAndPull setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [buttonForPushAndPull setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    
    
    
    [_headView addSubview:buttonForPushAndPull];
    [_headView addSubview:_collectionView];
    [self addSubview:_headView];
}

- (void)swipeDownAction
{
    [self headViewMoveDown];

}

- (void)coverViewAction
{
    [self headViewMoveUP];
}

- (void)actionForPullAndPush:(UIButton *)but
{
    but.selected = !but.selected;
    if(but.selected)
    {
        [self headViewMoveDown];
        
    }
    else
    {
        [self headViewMoveUP];
    }
    

}

- (void)headViewMoveUP
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _headView.top = -kHeaderOffH;
    _coverView.hidden = YES;
    [UIView commitAnimations];
}

- (void)headViewMoveDown
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _headView.top = 64;
    _coverView.hidden = NO;
    [UIView commitAnimations];

}

- (void)configureMainCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    _mainCollectionView = [[PosterCollectionView alloc] initWithFrame:CGRectMake(0, kPosterColletionViewY, kwth, kPosterColletionViewH) collectionViewLayout:flowLayout];
    _mainCollectionView.cellWidth = kwth * 3/4;
    _mainCollectionView.cellHeight = kPosterColletionViewH;
    
  
   // _mainCollectionView.models = _models;
    
    [self addSubview:_mainCollectionView];

}


- (void)setModels:(NSArray *)models
{
    _models = models;
    _mainCollectionView.models = _models;
    _collectionView.models = _models;
}





- (void)configureBottomTitleLabel
{
    _bottomTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kBottomTitleLableY, kwth, kBottomTitleLableH)];
    
    _bottomTitleLabel.backgroundColor = [UIColor yellowColor];
    
    [self addSubview:_bottomTitleLabel];

}

@end
